﻿Imports System.Configuration
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration.Provider
Imports System.Windows.Forms
Imports System.Collections.Specialized
Imports Microsoft.Win32
Imports System.Xml

''' <summary>
''' Settings class to allow for settings management from a database table.
''' Script to create table:
''' 
'''IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SystemSettings]') AND type in (N'U'))
'''DROP TABLE [SystemSettings]
'''go
'''CREATE TABLE [SystemSettings](
'''	[SettingId] [int] IDENTITY(1,1) NOT NULL,
'''	[SettingName] [varchar](500) NOT NULL,
'''	[SettingValue] [nvarchar](max) NULL,
'''	[Description] [nvarchar](2000) NULL,
''' CONSTRAINT [SystemSettings_pk] PRIMARY KEY ([SettingId]),
''' CONSTRAINT [SystemSettings_uq] UNIQUE NONCLUSTERED ([SettingName])
''')
'''go
''' 
''' </summary>
''' <remarks></remarks>
Public Class DBSettingsProvider
    Inherits SettingsProvider

    Private _applicationName As String

    Public Overrides Sub Initialize(ByVal name As String, ByVal col As NameValueCollection)
        MyBase.Initialize(Me.ApplicationName, col)
    End Sub

    Public Overrides Property ApplicationName As String
        Get
            If String.IsNullOrEmpty(_applicationName) Then
                If Application.ProductName.Trim.Length > 0 Then
                    _applicationName = Application.ProductName
                Else
                    Dim fi As New IO.FileInfo(Application.ExecutablePath)
                    _applicationName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)
                End If
            End If

            Return _applicationName
        End Get
        Set(ByVal value As String)
            _applicationName = value
        End Set
    End Property

    Public Overrides Function GetPropertyValues( _
                    ByVal context As System.Configuration.SettingsContext, _
                    ByVal collection As System.Configuration.SettingsPropertyCollection) As System.Configuration.SettingsPropertyValueCollection

        'Create new collection of values
        Dim values As SettingsPropertyValueCollection = New SettingsPropertyValueCollection()

        'Iterate through the settings to be retrieved
        For Each setting As SettingsProperty In collection
            Dim value As SettingsPropertyValue = New SettingsPropertyValue(setting)
            value.IsDirty = False
            value.SerializedValue = GetValue(setting)
            values.Add(value)
        Next
        Return values


    End Function

    Public Overrides Sub SetPropertyValues( _
                ByVal context As System.Configuration.SettingsContext, _
                ByVal collection As System.Configuration.SettingsPropertyValueCollection)

        Dim lst As List(Of SystemSetting) = New List(Of SystemSetting)

        For Each propval As SettingsPropertyValue In collection

            Dim seting As SystemSetting = SystemSettingDataUtils.findOne("SettingName={0}", propval.Name)
            If seting Is Nothing Then
                seting = SystemSettingFactory.Create
            End If

            seting.SettingName = propval.Name
            seting.SettingValue = CStr(propval.PropertyValue)
            lst.Add(seting)
        Next

        Dim sdb As New SystemSettingDBMapper
        sdb.saveList(lst)

    End Sub

    Private Function GetValue(ByVal setting As SettingsProperty) As Object

        Dim seting As SystemSetting = SystemSettingDataUtils.findOne("SettingName={0}", setting.Name)

        If seting IsNot Nothing Then
            Return seting.SettingValue
        Else
            If Not setting.DefaultValue Is Nothing Then
                Return setting.DefaultValue.ToString
            Else
                Return String.Empty
            End If

        End If

    End Function

End Class
